package org.ihelpuoo.orm.handler.map;

import java.lang.reflect.Field;
import java.lang.reflect.Method;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.ihelpuoo.orm.filter.Filter;
import org.ihelpuoo.orm.filter.FieldFilter;
import org.ihelpuoo.orm.handler.FieldHandlers;
import org.ihelpuoo.orm.handler.map.MapReadArrayFieldHandler;
import org.ihelpuoo.orm.handler.map.MapReadObjectFieldHandler;
import org.ihelpuoo.orm.handler.map.MapReadListFieldHandler;
import org.ihelpuoo.orm.handler.map.MapReadMapFieldHandler;
import org.ihelpuoo.orm.handler.map.MapReadNormalFieldHandler;
import org.ihelpuoo.orm.handler.map.MapSaveArrayFieldHandler;
import org.ihelpuoo.orm.handler.map.MapSaveObjectFieldHandler;
import org.ihelpuoo.orm.handler.map.MapSaveListFieldHandler;
import org.ihelpuoo.orm.handler.map.MapSaveMapFieldHandler;
import org.ihelpuoo.orm.handler.map.MapSaveNormalFieldHandler;
import org.ihelpuoo.orm.handler.MicroObjectProcessor;
import org.ihelpuoo.lang.ObjectUtils;
import org.ihelpuoo.orm.IIIException;
import org.ihelpuoo.orm.IIIManual;
import org.ihelpuoo.orm.ObjectRule;
import org.ihelpuoo.util.Pager;

public class MapProcessor extends MicroObjectProcessor
{

	public MapProcessor(IIIManual manual)
	{
		super(manual);
		this.getFieldHandlers = new FieldHandlers(new MapReadNormalFieldHandler(this),
				new MapReadListFieldHandler(this),new MapReadArrayFieldHandler(this),
				new MapReadMapFieldHandler(this),new MapReadObjectFieldHandler(this));
		this.setFieldHandlers = new FieldHandlers(new MapSaveNormalFieldHandler(this),
				new MapSaveListFieldHandler(this),new MapSaveArrayFieldHandler(this),
				new MapSaveMapFieldHandler(this),new MapSaveObjectFieldHandler(this));
	}

	@Deprecated
	@Override
	public boolean delete(ObjectRule r, Object src, Object obj, FieldFilter ignore) throws IIIException
	{
		throw new IIIException(this.getClass().getName() + " do not support deleteBean()!");
	}

	@Deprecated
	@Override
	public int delete(ObjectRule r, Object src, List list, int off, int len) throws IIIException
	{
		throw new IIIException(this.getClass().getName() + " do not support clearBeans()!");
	}

	@SuppressWarnings("unchecked")
	@Override
	public Object insert(ObjectRule r, Object src, Object obj, FieldFilter actived,
			FieldFilter locked) throws IIIException
	{
		if (src instanceof Map)
		{
			Field keyField = r.getKeyField((Map)src);
			if (null == keyField)
			{
				throw new IIIException("Fail to find key field for '" + r.getType().getName()
						+ "'");
			}
			try
			{
				Method getter = ObjectUtils.getGetter(r.getType(),keyField);
				String key = getter.invoke(obj,(Object[])null).toString();
				Map<String, Object> map = new HashMap<String, Object>();
				update(r,map,obj,actived,locked);
				((Map)src).put(key,map);
			}
			catch (Exception e)
			{
				throw IIIException.wrap(e);
			}
		}
		else if (src instanceof List)
		{
			Map map = new HashMap<String, Object>();
			update(r,map,obj,actived,locked);
			((List)src).add(map);
		}
		else
		{
			throw new IIIException("If you wanna insert a bean \"" + obj.getClass().getName()
					+ "\" the src object can only be List or Map!");
		}
		return obj;
	}

	@Deprecated
	@Override
	public List query(ObjectRule r, Object src, Class beanType, Filter filter,
			FieldFilter actived, FieldFilter locked, Pager pager, Object refer)
			throws IIIException
	{
		throw new IIIException(this.getClass().getName() + " do not support queryBeanList()!");
	}
}
